package com.yanqu.road.dao.impl.beautyescort;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.beautyescort.EscortBeautyData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class EscortBeautyDataDaoImpl extends TempDataObjectDao<EscortBeautyData> {

    public static String getEscortBeautyDataSql = "select * from `t_u_escort_beauty` where `userId`=?";
    public Map<Integer, EscortBeautyData> getEscortBeautyData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, EscortBeautyData> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getEscortBeautyDataSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                EscortBeautyData data =  parse(rs);
                result.put(data.getBeautyId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String insertEscortBeautyDataSql = "insert into `t_u_escort_beauty`(userId,beautyId,escortTimes,buyEscortTimes,robDartTimes,buyRobDartTimes,resetTime) values (?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(EscortBeautyData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBeautyId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEscortTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBuyEscortTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRobDartTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBuyRobDartTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getResetTime()));
        if (getDb().executeNoneQuery(insertEscortBeautyDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateEscortBeautyDataSql = "update `t_u_escort_beauty` set escortTimes=?,buyEscortTimes=?,robDartTimes=?,buyRobDartTimes=?,resetTime=? where userId=? and beautyId=?";
    @Override
    protected boolean updateData(EscortBeautyData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getEscortTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBuyEscortTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRobDartTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBuyRobDartTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getResetTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBeautyId()));
        if (getDb().executeNoneQuery(updateEscortBeautyDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected EscortBeautyData parseData(ResultSet rs) throws SQLException {
        EscortBeautyData data = new EscortBeautyData();
        data.setUserId(rs.getLong("userId"));
        data.setBeautyId(rs.getInt("beautyId"));
        data.setEscortTimes(rs.getInt("escortTimes"));
        data.setBuyEscortTimes(rs.getInt("buyEscortTimes"));
        data.setRobDartTimes(rs.getInt("robDartTimes"));
        data.setBuyRobDartTimes(rs.getInt("buyRobDartTimes"));
        data.setResetTime(rs.getLong("resetTime"));

        return data;
    }
}
